Page({
  data: {

    startdates: '请选择',
    starttimes: '请选择',
    //enddates: '请选择',
    endtimes: '请选择',
    limitdata:[],
  },
  onLoad: function (option) {

    const db = wx.cloud.database()
    db.collection("limits").where({
    }).get({
      success: res => {
        this.setData({
          limitdata: res.data,
        })
      }
    })
  },
  

  //  点击时间组件确定事件  
  bindstartTimeChange: function (e) {
    this.setData({
      starttimes: e.detail.value
    })
  },
  bindendTimeChange: function (e) {
    this.setData({
      endtimes: e.detail.value
    })
  },
  //  点击日期组件确定事件  
  bindstartDateChange: function (e) {
    this.setData({
      startdates: e.detail.value
    })
  },
  // bindendDateChange: function (e) {
  //   this.setData({
  //     enddates: e.detail.value
  //   })
  // },
  //提交预约
  tosumbit: function () {
    var that = this;
    //验证日期
    if (that.data.startdates.length < 6) {
      wx.showModal({
        title: '提示',
        content: "请选择起始日期"
      })
      return
    }
    // if (that.data.enddates.length < 6) {
    //   wx.showModal({
    //     title: '提示',
    //     content: "请选择结束日期"
    //   })
    //   return
    // }
    //验证时间
    if (that.data.starttimes.length < 4) {
      wx.showModal({
        title: '提示',
        content: "请选择起始时间"
      })
      return
    }
    if (that.data.endtimes.length < 4) {
      wx.showModal({
        title: '提示',
        content: "请选择结束时间"
      })
      return
    }
    //信息无误，进行提交
    const db = wx.cloud.database()//打开数据库连接
    db.collection("limits").add({
      data: {

        startdates: that.data.startdates,
        starttimes: that.data.starttimes,
        //enddates: that.data.enddates,
        endtimes: that.data.endtimes,
       
      }, success: res => {
        wx.showToast({
          title: '设置成功',
          duration: 6000,
          mask: true
        }),
          wx.switchTab({
            url: '../index/index',
          })
      }, fail: err => {
        wx.showToast({
          title: '设置失败，请重试',
        })
      }
    })
  },
  //取消
  tocancel: function (e) {
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
          // 用户点击了确定 可以调用删除方法了
          let id = e.currentTarget.dataset.id
          const db = wx.cloud.database();
          db.collection("limits").doc(id).remove({
            success: res => {

              wx.showToast({
                title: '删除成功',
              })
              //删除成功后刷新当前页面
              getCurrentPages()[getCurrentPages().length - 1].onLoad()
              //删除成功重新加载
            }, fail: err => {
              wx.showToast({
                title: '删除失败',
              })
            }
          })
        }
      }
    })
  },

})